package bid.yanhj.yanhaijiang.viewpager;

import android.support.annotation.NonNull;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;

import java.util.List;

public class MainVpAdpter extends PagerAdapter {

    private List<View> views;

    // command + n 快速构造方法
    // see https://www.cnblogs.com/dubo-/p/5571515.html
    public MainVpAdpter(List<View> views) {
        this.views = views;
    }

    /**
     * 共显示多少view
     *
     * @return
     */
    @Override
    public int getCount() {
        return views.size();
    }

    /**
     * 是否一致 一致显示在屏幕上
     * @param view
     * @param object
     * @return
     */
    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
        return view == object;
    }

    /**
     * 每页显示的内容
     * @param container
     * @param position
     * @return
     */
    @NonNull
    @Override
    public Object instantiateItem(@NonNull ViewGroup container, int position) {
        View view = views.get(position);
        // container  ViewPager
        container.addView(view);
        return view;
    }

    /**
     *  view 滑出屏幕 销毁
     * @param container
     * @param position
     * @param object
     */
    @Override
    public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
        container.removeView(views.get(position));
    }
}
